package com.nand.module.function.ui;

import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.lzy.okgo.model.HttpParams;
import com.nand.library.base.base.BaseActivity;
import com.nand.library.base.bean.ResponseBean;
import com.nand.library.base.http.MyCallback;
import com.nand.library.base.http.OkGoUtils;
import com.nand.module.function.R;
import com.nand.module.function.adapter.ReviewlistAdapter;
import com.nand.module.function.bean.IqcDetailsBean;
import com.nand.module.function.constant.URL;
import com.nand.module.function.utils.LogUtils;
import com.nand.module.function.utils.ToolUtils;
import com.nand.module.function.view.CommonPopupWindow;
import com.nand.module.function.view.AutofitTextView;

import java.util.List;

/**
 * created by 86185
 * on 2020/1/6
 */
@Route(path = "/function/iqc/details")
public class IqcDetailsActivity extends BaseActivity {

    private TextView mTvDjbh;
    private AutofitTextView mTvWlpcm;
    private TextView mTvWlbm;
    private TextView mTvWlmc;
    private TextView mTvGys;
    private TextView mTvBhgsl;
    private TextView mTvQxcd;
    private TextView mTvClyj;
    private String oid;
    private String Url = "";
    private int type;
    private TextView mTvSl;
    private TextView mTvWllb;
    private TextView mTvIqcMj;
    private TextView mTvShpcm;
    private TextView mTvYxj,mTvKf;
    private TextView mTvBz;
    private TextView mTvJybg;
    private RecyclerView mReviewRecycer;
    private ReviewlistAdapter mAdapter;
    private CommonPopupWindow.LayoutGravity layoutGravity;
    private CommonPopupWindow windowContent;
    private TextView horiText;

    @Override
    protected int initLayout() {
        return R.layout.activity_iqc_details;
    }

    @Override
    protected void initView() {
        oid = getIntent().getStringExtra("oid");
        type = getIntent().getIntExtra("type", 0);

        mTvDjbh = findViewById(R.id.tv_djbh);
        mTvWlpcm = findViewById(R.id.tv_wlpcm);
        mTvWlbm = findViewById(R.id.tv_wlbm);
        mTvWlmc = findViewById(R.id.tv_wlmc);
        mTvGys = findViewById(R.id.tv_gys);
        mTvBhgsl = findViewById(R.id.tv_bhgsl);
        mTvQxcd = findViewById(R.id.tv_qxcd);
        mTvClyj = findViewById(R.id.tv_clyj);
        mTvSl = findViewById(R.id.tv_sl);
        mTvWllb = findViewById(R.id.tv_wllb);
        mTvIqcMj = findViewById(R.id.tv_iqcmj);
        mTvShpcm = findViewById(R.id.tv_shpcm);
        mTvYxj = findViewById(R.id.tv_yxj);
        mTvKf = findViewById(R.id.tv_kf);
        mTvBz = findViewById(R.id.tv_bz);
        mTvJybg = findViewById(R.id.tv_jybg);
        initPop();
        switch (type) {
            case 0:
                mActionBar().setCenterText("来料单详情");
                Url = URL.QUERY_IQC_CHECK;
                findViewById(R.id.ll_view).setVisibility(View.GONE);
                break;
            case 2:
                mActionBar().setCenterText("不合格处理单");
                findViewById(R.id.ll_view2).setVisibility(View.GONE);
                Url = URL.QUERY_IQC_NG;
                mReviewRecycer = findViewById(R.id.recycler2);
                mReviewRecycer.setLayoutManager(new LinearLayoutManager(this));
                mAdapter = new ReviewlistAdapter(IqcDetailsActivity.this);
                mReviewRecycer.setAdapter(mAdapter);

                mAdapter.setOnItemClickListener(new ReviewlistAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {

                    }

                    @Override
                    public void onXxClick(int position, String context, TextView a) {
                        layoutGravity=new CommonPopupWindow.LayoutGravity(CommonPopupWindow.LayoutGravity.ALIGN_LEFT| CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                        layoutGravity.setHoriGravity(CommonPopupWindow.LayoutGravity.ALIGN_RIGHT);
                        layoutGravity.setVertGravity(CommonPopupWindow.LayoutGravity.TO_BOTTOM);
                        windowContent.showBashOfAnchor(a, layoutGravity, 0, 0);
                        horiText.setText(context);
                    }
                });
                break;
        }
    }

    private void initPop() {
        windowContent = new CommonPopupWindow(this, R.layout.item_show_content, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
            @Override
            protected void initView() {
                View view=getContentView();
                horiText = view.findViewById(R.id.hori_text);
            }

            @Override
            protected void initEvent() {}
        };
    }

    @Override
    protected boolean isActionBar() {
        return true;
    }


    @Override
    protected void initData() {
        HttpParams params = new HttpParams();
        params.put("oid", oid);
        OkGoUtils.getInstance().post(this,Url , params, 1, true, new MyCallback() {
            @Override
            public void onSuccess(ResponseBean response, int id) {
                if (200 == response.getCode()) {
                    String s = response.getDataInfo().toString();
                    LogUtils.e("data==========="+s);
                    IqcDetailsBean listBeans = JSON.parseObject(s, IqcDetailsBean.class);
                    List<IqcDetailsBean.DataListBean> dataList = listBeans.getDataList();

                    if (0 < dataList.size()) {
                        IqcDetailsBean.DataListBean bean = dataList.get(0);

                        switch (type){
                            case 0:
                                mTvDjbh.setText(bean.getSendCheckID());
                                mTvWlpcm.setTextSize(ToolUtils.sp2px(mContext,16));
                                mTvWlpcm.setText(bean.getBatchID());
                                mTvWlbm.setText("" + bean.getPartId());
                                mTvWlmc.setText("" + bean.getPartName());
                                mTvGys.setText(bean.getSupplierName());
                                mTvSl.setText(bean.getNumber());
                                mTvKf.setText(bean.getStoreName());
                                int type = bean.getType();
                                switch (type){
                                    case 1:
                                        mTvWllb.setText("成品");
                                        break;
                                    case 2:
                                        mTvWllb.setText("半成品");
                                        break;
                                    default:
                                        mTvWllb.setText("原材料");
                                        break;
                                }

                                int iqcNCheck = bean.getIqcNCheck();

                                if (0 == iqcNCheck){
                                    mTvIqcMj.setText("免检");
                                }else {
                                    mTvIqcMj.setText("不免检");
                                }
                                String priority = bean.getPriority();
                               /* if ("1".equals(priority)) {
                                    mTvYxj.setText("低");
                                } else if ("2".equals(priority)) {
                                    mTvYxj.setText("中");
                                } else {
                                    mTvYxj.setText("高");
                                }*/
                                mTvYxj.setText(priority);

                                int packageReport = bean.getPackageReport();
                                if (0 == packageReport) {
                                    mTvBz.setText("不完整");
                                } else {
                                    mTvBz.setText("完整");
                                }

                                int checkReport = bean.getCheckReport();
                                if (0 == checkReport) {
                                    mTvJybg.setText("不完整");
                                } else {
                                    mTvJybg.setText("完整");
                                }

                                mTvShpcm.setText(bean.getSupplierBatchID());

                                break;
                            case 2:
                                mTvDjbh.setText(bean.getIqcNGDataID());
                                mTvWlpcm.setTextSize(ToolUtils.sp2px(mContext,16));
                                mTvWlpcm.setText("" + bean.getIqcNGBatchID());
                                mTvWlbm.setText("" + bean.getPartId());
                                mTvWlmc.setText("" + bean.getPartName());
                                mTvGys.setText(bean.getSupplierName());
                                mTvBhgsl.setText("" + bean.getNgNumber());


                                List<IqcDetailsBean.DataListBean.LogDetailBean> logDetail = bean.getLogDetail();


                                if (0 < logDetail.size()){
                                    IqcDetailsBean.DataListBean.LogDetailBean detailBean = logDetail.get(0);
                                    mTvQxcd.setText(detailBean.getDefectLevel());
                                    mTvClyj.setText(detailBean.getIqcNgContent());
                                }
                                mAdapter.setData(logDetail);
                                break;
                        }

                    }
                } else {
                    showToast(response.getMsg());
                }
            }


            @Override
            public void onError(Throwable e, int id) {

            }
        });
    }
}
